package yyds.sniarbtej.transformer;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;

public final class HideAgent extends BaseClassTransformer {
    public String canTransformClassName() {
        return "java/lang/Class";
    }

    public byte[] rewrite(byte[] in) {
        return transform(in, new ClassRewriter() {
            @Override
            public void rewriteMethod(MethodNode node) {
                if ("forName".equals(node.name) && node.desc.startsWith("(Ljava/lang/String;")) {
                    InsnList insn = new InsnList();
                    insn.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    /* hideAgentClass */
                    insn.add(new MethodInsnNode(Opcodes.INVOKESTATIC, methodOverride, "C2", "(Ljava/lang/String;)V", false));
                    node.instructions.insert(insn);
                }
            }
        });
    }
}
